package com.weiadmin.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weiadmin.common.BaseController;
import com.weiadmin.common.PageParam;
import com.weiadmin.dao.SlideDao;
import com.weiadmin.model.system.SlideModel;
import com.weiadmin.service.SlideService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 *轮播图模块
 * @author: shizhongyu
 * @date: 2023年05月31日 11:31
 */
@RestController
@RequestMapping("/admin/slide")
public class SlideController extends BaseController {
    @Autowired
    SlideDao dao;

    @Autowired
    SlideService slideService;
    @GetMapping("/index")
    public ModelAndView index(Model model) {
        ModelAndView modelAndView = display("slide/index");
        return modelAndView;
    }
    @GetMapping("/list")
    public ResponseEntity<?> list(PageParam pageParam)
    {
//        Page<Slide> pager = new Page<>(pageParam.getPage(),pageParam.getLimit());
//        Page<Slide> slidePage = dao.selectPage(pager, null);
        Page<SlideModel> result =  slideService.getList(pageParam);
        return page("请求成功",result);
//        return page("请求成功",result.getRecords(),result.getTotal());
    }

    @GetMapping("/add")
    public ModelAndView add(Model model) {
        ModelAndView modelAndView = display("slide/add");
        return modelAndView;
    }

    @GetMapping("/edit")
    public ModelAndView edit(Model model,String id) {
        ModelAndView modelAndView = display("slide/edit");
        SlideModel item =  dao.selectById(id);
        model.addAttribute("item",item);
        return modelAndView;
    }

    @PostMapping("/addPost")
    public ResponseEntity<?> addPost(@RequestBody Map<String,String> params)
    {
        SlideModel slide = new SlideModel();
        slide.setName(params.get("name"));
        slide.setRemark(params.get("remark"));
        dao.insert(slide);
        return success("添加成功！");
    }

    @PostMapping("/editPost")
    public ResponseEntity<?> editPost(@RequestBody Map<String,String> params)
    {
        SlideModel slide = new SlideModel();
        slide.setId(params.get("id"));
        slide.setName(params.get("name"));
        slide.setRemark(params.get("remark"));
        dao.updateById(slide);
        return success("保存成功！");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<?> delete(@PathVariable String id)
    {
        if(StringUtils.isEmpty(id)){
            return error("ID不能为空！");
        }
        int result = dao.deleteById(id);
        if(result>0){
            return success("删除成功！");
        }else{
            return error("删除失败,请稍后再试！");
        }
    }

}
